<template>
  <div class="app-container">
    <div class="card-wrap padding20 flex">

      <div v-for="(item, index) in tabs" :key="index" @click="tabClick(index)" :class="item.check?'tab-active':'tab-normal'">
        {{ item.name }}
      </div>

    </div>
    <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">

      <el-col :span="6">
        <el-form-item label="车牌号/车架号:" prop="carNoVinNo">
          <el-input maxlength="30" v-model="queryParams.carNoVinNo" clearable placeholder="请输入车牌号/车架号"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:" prop="modelId">
          <linkage-select v-model="linkageList" placeholder="请选择品牌/车系/车型" :check-strictly="false" style="width: 100%" @changeNode="linkageChange" />
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆产权:" prop="propertyRightName">

          <el-select placeholder="请选择车辆产权" filterable v-model="queryParams.propertyRightName" clearable style="width: 100%">
            <template v-for="(item, index) in companyOriginList">
              <el-option :label="item.deptName" :value="item.deptName" :key="index"></el-option>
            </template>
          </el-select>

        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆归属:" prop="ascriptionId">

          <el-select placeholder="请选择车辆归属" filterable @change="belongChange" v-model="queryParams.ascriptionId" clearable style="width: 100%">
            <template v-for="(item, index) in belongCompanyList">
              <el-option :label="item.deptName" :value="item.deptId" :key="index"></el-option>
            </template>
          </el-select>

        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆大区:" prop="cityId">

          <RegionCityLink v-model="queryParams.citydata" placeholder="请选择车辆大区" ref="citySelect" :showAllLevels="false" :defaultSelect="selIds" @change="citySelectionChange"></RegionCityLink>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆运营城市:" prop="operationsName">
          <el-input maxlength="30" v-model="queryParams.operationsName" clearable placeholder="请输入运营城市"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="产品类型:" prop="contractType">
          <el-select placeholder="请选择产品类型" filterable v-model="queryParams.contractType" clearable>
            <template v-for="dict in dict.type.quota_rent_type">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆状态:" prop="businessType">
          <el-select placeholder="请选择车辆状态" filterable v-model="queryParams.businessType" clearable>
<!--            <el-option v-for="item in businessTypeList" :key="item.value" :label="item.label" :value="item.value">-->
<!--            </el-option>-->
              <template v-for="dict in dict.type.car_business_type">
                  <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="GPS品牌:" prop="gpsBrand">
          <el-input maxlength="30" v-model="queryParams.gpsBrand" clearable placeholder="请输入GPS品牌"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="客户名称:" prop="customerName">
          <el-input maxlength="30" v-model="queryParams.customerName" clearable placeholder="请输入客户名称"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="GPS时效:" prop="gpsAging">
          <el-select v-model="queryParams.gpsAging" clearable>
            <el-option label="有效" :value="0"></el-option>
            <el-option label="无效" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="GPS到期时间:" prop="renewEndTime">
          <el-date-picker v-model="queryParams.renewEndTime" type="date" placeholder="请选择" value-format='yyyy-MM-dd' clearable>
          </el-date-picker>
        </el-form-item>
      </el-col>

      <!--            <el-col :span="24">-->
      <!--                <el-form-item label="查询时间:">-->
      <!--                    <ShortcutDate @isButIdx="isButIdx"-->
      <!--                                  v-model="checked" @dateChangeType="changeShortcutDate">-->

      <!--                        <el-date-picker v-model="dateValue" type="daterange" range-separator="-"-->
      <!--                                        start-placeholder="开始日期" unlink-panels-->
      <!--                                        end-placeholder="结束日期" value-format='yyyy-MM-dd' clearable-->
      <!--                                        @change='changeDate'>-->
      <!--                        </el-date-picker>-->

      <!--                    </ShortcutDate>-->
      <!--                </el-form-item>-->
      <!--            </el-col>-->

    </SearchContent>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button type="primary" size="mini" @click="handleExport" v-hasPermi="['GPSManage:GPSRenewalManage:export']">导出
        </el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column min-width="130" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button size="mini" type="text" @click="handleDetail(scope.row)" v-hasPermi="['GPSManage:GPSRenewalManage:view']">查看
              </e-button>
              <e-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['GPSManage:GPSRenewalManage:delete']">删除
              </e-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column show-overflow-tooltip min-width="210" label="车辆产权" align="center" prop="propertyRightName">
            <template v-slot="{row}">
              {{row.propertyRightName|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="210" label="车辆归属" align="center" prop="ascription">
            <template v-slot="{row}">
              {{row.ascription|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="车辆大区" align="center" prop="region">
            <template v-slot="{row}">
              {{row.region|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="车辆运营城市" align="center" prop="operationsName">
            <template v-slot="{row}">
              {{row.operationsName|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="车牌号" align="center" prop="carNo">
            <template v-slot="{row}">
              {{row.carNo|filterNull}}
              <!--                            <e-button size="mini" type="text">{{row.carNo|filterNull}}-->
              <!--                            </e-button>-->
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="190" label="车架号" align="center" prop="vinNo">
            <template v-slot="{row}">
              {{row.vinNo|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="190" label="车辆状态" align="center" prop="businessType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.car_business_type" :value="scope.row.businessType" />
            </template>
<!--            <template v-slot="{row}">-->
<!--              {{row.vinNo|filterNull}}-->
<!--            </template>-->
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="180" label="品牌/车系/车型" align="center" prop="brandName">
            <template v-slot="{row}">
              {{row.brandName|filterNull}}/{{row.seriesName|filterNull}}/{{row.modelName|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="客户名称" align="center" prop="customerName">
            <template v-slot="{row}">
              {{row.customerName|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="产品类型" align="center" prop="contractType">

            <template slot-scope="scope">
              <dict-tag :options="dict.type.quota_rent_type" :value="scope.row.contractType" />
            </template>

          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="客户等级" align="center" prop="customerLevel">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.customer_level" :value="scope.row.customerLevel" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="90" label="联系人" align="center" prop="primaryContact">
            <template v-slot="{row}">
              {{row.primaryContact|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="150" label="联系电话" align="center" prop="primaryContactPhone">
            <template v-slot="{row}">
              {{row.primaryContactPhone|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="150" label="合同实际开始日期" align="center" prop="contractStartTime">
            <template v-slot="{row}">
              {{row.contractStartTime|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="150" label="合同实际结束日期" align="center" prop="contractEndTime">
            <template v-slot="{row}">
              {{row.contractEndTime|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="115" label="GPS品牌" align="center" prop="gpsBrand">
            <template v-slot="{row}">
              {{row.gpsBrand|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="190" label="GPS设备编号" align="center" prop="equipmentNo">
            <template v-slot="{row}">
              {{row.equipmentNo|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="90" label="设备类型" align="center" prop="equipmentType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.gps_device_type" :value="scope.row.equipmentType" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="150" label="GPS安装时间" align="center" prop="installTime">
            <template v-slot="{row}">
              {{row.installTime|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="150" label="GPS到期时间" align="center" prop="installExpireTime">
            <template v-slot="{row}">
              {{row.installExpireTime|filterNull}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="90" label="GPS时效" align="center" prop="gpsAging">
            <template v-slot="{row}">
<!--              {{row.gpsAging|filterNull}}-->
              {{0==row.gpsAging?'有效':1==row.gpsAging?'失效':'--'}}
            </template>
          </el-table-column>

        </template>
      </table-page>

    </div>
  </div>
</template>

<script>
import SearchContent from "@/components/SearchContent/index.vue";
import LinkageSelect from "@/views/components/linkageSelect.vue";
import CitySelect from "@/views/components/citySelect.vue";
import ShortcutDate from "@/views/components/ShortcutDate/index.vue";
import { listDept } from "@/api/system/dept";
import RegionCityLink from "@/views/components/provinceCity/regionCityLink.vue";
import { gpsRenewList, gpsRenewDel } from "@/api/gps/gpsList";

export default {
  name: "GPSRenewalList",
  components: {
    RegionCityLink,
    ShortcutDate,
    CitySelect,
    LinkageSelect,
    SearchContent,
  },
  dicts: ['gps_alarm_type', 'quota_rent_type', 'gps_workorder_type', 'gps_work_order_progress', 'vehicle_category', 'gps_device_type', 'customer_type', 'customer_level', 'gps_state', 'car_business_type'],
  data() {
    return {
      tabs: [{
        name: '当前数据',
        count: 0,
        check: true
      }, {
        name: '历史数据查询',
        count: 0,
        check: false
      }],
      checked: "",
      companyOriginList: [],
      dateValue: [],
      citySelection: [],
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isHistoryData: 0,
        carNoVinNo: null,
        brandId: null,
        seriesId: null,
        modelId: null,
        brandName: null,
        seriesName: null,
        modelName: null,
        propertyRightId: null,
        propertyRightName: null,
        ascriptionId: null,
        ascription: null,
        regionId: null,
        region: null,
        provinceName: null,
        provinceId: null,
        cityId: null,
        cityName: null,
        operationsName: null,
        gpsWorkProgress: null,
        // companyName:null,
        contractType: null,
        businessType: null,
        gpsType: null,
        gpsBrand: null,
        customerName: null,
        alarmType: null,
        queryStartTime: null,
        queryEndTime: null,
        gpsAging: null,
        renewEndTime: null,
        citydata: []
      },

      total: 0,
      dataList: [],
      belongCompanyList: [],
      selIds: [],
      linkageList: []

    };
  },
  created() {
    this.getList();
    this.getCompanyOrigin();
  },
  activated() {
    this.getList()
    this.getCompanyOrigin();
  },
  methods: {
    getBusinessType(type) {
      return this.businessTypeList.find(item => {
        return item.value == type
      })?.label ?? '--'
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      this.queryParams.queryStartTime = this.dateValue && this.dateValue[0] ? this.dateValue[0] : ''
      this.queryParams.queryEndTime = this.dateValue && this.dateValue[1] ? this.dateValue[1] : ''

      gpsRenewList(this.queryParams).then(res => {
        if (res.code == "200") {
          const { rows, total } = res
          this.dataList = rows
          this.total = total
          this.loading = false;

        }
      });
    },
    tabClick(index) {
      this.tabs.forEach((item, i) => {
        item.check = index == i;
      })

      this.queryParams.isHistoryData = index

      this.handleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.timeRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        isHistoryData: 0,
        carNoVinNo: null,
        brandId: null,
        seriesId: null,
        modelId: null,
        brandName: null,
        seriesName: null,
        modelName: null,
        propertyRightId: null,
        propertyRightName: null,
        ascriptionId: null,
        ascription: null,
        regionId: null,
        region: null,
        provinceName: null,
        provinceId: null,
        cityId: null,
        cityName: null,
        operationsName: null,
        gpsWorkProgress: null,
        // companyName:null,
        contractType: null,
        businessType: null,
        gpsType: null,
        gpsBrand: null,
        customerName: null,
        alarmType: null,
        queryStartTime: null,
        queryEndTime: null,
        gpsAging: null,
        renewEndTime: null,
        citydata: []
      },
        this.checked = -1;
      this.selIds = []
      this.citySelection = []
      this.dateValue = []
      this.linkageList = []
      this.citySelectionChange([])
      // this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/car/gpsRenew/export', {
        ...this.queryParams
      }, `GPS续费管理.xlsx`)
    },
    handleDetail(row) {
      this.$router.push({
        name: 'GPSRenewalDetail',
        query: { gpsRenewId: row.gpsRenewId, carId: row.carId }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('确定删除？').then(() => {
        this.loading = true;
        return gpsRenewDel(row.gpsRenewId);
      }).then(() => {
        this.loading = false;
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },

    isButIdx(idx) {
      this.checked = idx;
      // this.dateValue = [];
    },
    changeDate(val) {
      this.checked = -1;
      if (val) {
        this.queryParams.queryStartTime = val[0]
        this.queryParams.queryEndTime = val[1]
      } else {
        this.queryParams.queryStartTime = ""
        this.queryParams.queryEndTime = ""
      }
    },
    changeShortcutDate(val) {
      if (val) {
        this.dateValue = val
        this.queryParams.queryStartTime = val[0]
        this.queryParams.queryEndTime = val[1]
      } else {
        this.queryParams.queryStartTime = ""
        this.queryParams.queryEndTime = ""
      }
      console.log('changeShortcutDate', val, this.queryParams)
      // this.dateValue = []
      // this.queryParams.violationStartTime = val[0]
      // this.queryParams.violationEndTime = val[1]
      // this.getList()
    },

    async linkageChange(val) {
      this.linkageList = val
      if (val) {
        this.queryParams.brandId = val[0] ? val[0].id : ''
        this.queryParams.seriesId = val[1] ? val[1].id : ''
        this.queryParams.modelId = val[2] ? val[2].id : ''
        this.queryParams.brandName = val[0] ? val[0].name : ''
        this.queryParams.seriesName = val[1] ? val[1].name : ''
        this.queryParams.modelName = val[2] ? val[2].name : ''
      } else {
        this.queryParams.brandId = null
        this.queryParams.seriesId = null
        this.queryParams.modelId = null
        this.queryParams.brandName = null
        this.queryParams.seriesName = null
        this.queryParams.modelName = null
      }
      this.$forceUpdate()
    },

    getCompanyOrigin() {
      listDept(
        {
          deptType: '2',//组织类型（1部门 2公司）
          regionId: null,//所在城市（省市区id）
        }).then(res => {
          this.companyOriginList = res.data
          this.belongCompanyList = res.data
        })
    },

    getBelongCompany() {
      listDept(
        {
          deptType: '2',//组织类型（1部门 2公司）
          regionId: this.queryParams.cityId,//所在城市（省市区id）
        }).then(res => {
          this.belongCompanyList = res.data
        })
    },
    citySelectionChange(val) {
      if (val.length > 0) {
        this.queryParams.region = val[0].pathNodes[0]?.label
        this.queryParams.regionId = val[0].pathNodes[0]?.value
        this.queryParams.provinceName = val[0].pathNodes[1]?.label
        this.queryParams.provinceId = val[0].pathNodes[1]?.value
        this.queryParams.cityName = val[0].pathNodes[2]?.label
        this.queryParams.cityId = val[0].pathNodes[2]?.value
      } else {
        this.queryParams.regionId = null
        this.queryParams.region = null
        this.queryParams.provinceName = null
        this.queryParams.provinceId = null
        this.queryParams.cityId = null
        this.queryParams.cityName = null
      }

      let find = this.belongCompanyList.find(item => this.queryParams.ascriptionId == item.deptId);

      if (this.queryParams.cityId && find && this.queryParams.cityId != find.regionId.split("/")[1]) {
        this.queryParams.ascription = null;
        this.queryParams.ascriptionId = null;
      }


      this.belongCompanyList = []
      this.getBelongCompany();
    },

    //归属
    belongChange(val) {
      let find = this.belongCompanyList.find(item => val == item.deptId);
      if (find) {
        this.queryParams.ascription = find.deptName ?? null
        console.log('belongChange', find)

        this.selIds = [find.areaId,
        find.regionId && find.regionId.split("/")[0],
        find.regionId && find.regionId.split("/")[1]
        ];
      } else {
        this.selIds = []
      }


    },

  }
};
</script>


<style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__body .el-form {
    padding: 0 !important;
    margin-bottom: 0 !important;
  }
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: "";
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
